<?php
	class cache
	{
		public $count = array('set' => 0, 'get' => 0, 'check' => 0, 'delete' => 0);
		private $dir, $ext;
		
		public function __construct($dir = 'Cache/', $ext = '.cache.php')
		{
			$this->dir = $dir;
			$this->ext = $ext;
			return;
		}
			
		public function set($key, $data)
		{
			$this->count['set'] += 1;
			
			file_put_contents($this->dir . md5($key) . $this->ext, json_encode($data));
			return;
		}
		
		public function get($key)
		{
			if ( file_exists($this->dir . md5($key) . $this->ext) == true )
			{
				$this->count['get'] += 1;
				return json_decode(file_get_contents($this->dir . md5($key) . $this->ext), true);
			}
			else
			{
				return false;
			}
		}
		
		public function check($key, $timeout = 60)
		{
			$this->count['check'] += 1;
			
			if ( file_exists($this->dir . md5($key) . $this->ext) == true )
			{
				if ( time() - filemtime($this->dir . md5($key) . $this->ext) >= $timeout )
				{
					return false;
				}
				else
				{
					return true;
				}
			}
			else
			{
				return false;
			}
		}
		
		public function delete($key)
		{
			if ( file_exists($this->dir.md5($key).$this->ext) == true )
			{
				$this->count['delete'] += 1;
				
				unlink($this->dir.md5($key).$this->ext);
				return true;
			}
			else
			{
				return false;
			}
		}
	}
?>